package cn.good.yan.model.controller;

import cn.good.yan.model.service.UserService;
import io.jsonwebtoken.Jwts;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.nio.charset.StandardCharsets;

/**
 * 测试： 用户数据，来源于数据库
 *
 * @author shengren.yan
 * @create 2024-07-16
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @Resource
    public UserService userService;


    // 获取当前用户  -  得到jwt里的信息
    @GetMapping("/getUser")
    public Object getUser(Authentication authentication, HttpServletRequest request) {
        String head = request.getHeader("Authorization");
        String token = head.substring(head.indexOf("bearer") + 7);

        // test_key 这个在 JwtTokenStoreConfig 这里配置
        return Jwts.parser()
                .setSigningKey("test_key".getBytes(StandardCharsets.UTF_8))
                .parseClaimsJws(token).getBody();
//        return authentication.getPrincipal();

    }
    // [JWT内存增强器]
    // token 得到结果：
    // {"user_name":"admin","scope":["all"],"exp":1730073289,"enha":"infoxxxx","authorities":["admin"],"jti":"9dd4a7ad-8b48-43aa-bf11-75844139474b","client_id":"admin"}
    // 如果是 head 里，得到的是 dearer JWT生成的字符串

}
